﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SmartWeigh;
using SmartWeigh.ViewModel;
using SmartWeighMonitor.Model;
using SmartWeighMonitor.Utils;
using SmartWeighMonitor.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SmartWeighMonitor.Windows
{
    /// <summary>
    /// RecordWindow.xaml 的交互逻辑
    /// </summary>
    public partial class RecordWindow : Window
    {
        MonitorViewModel viewModel;
        MainViewModel mainViewModel;
        private String queryBillStatus = "0";
        string deviceCode = "";
        SynchronizationContext _syncContext = null;
        MainWindow main = null;
        public RecordWindow(MainWindow mainWindow)
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;
            mainViewModel = new MainViewModel();
            PoundList.DataContext = mainViewModel;
            viewModel = new MonitorViewModel();
            main = mainWindow;
            this.Loaded += Window_Loaded;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            resetQueryParamaters();
            LoadData();
        }

        public void setDeviceCode(String id)
        {
            deviceCode = id;
        }

        private void radio_click_status(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.RadioButton button = (System.Windows.Controls.RadioButton)sender;
            string tag = (string)button.Tag;
            this.queryBillStatus = tag;
            LoadData();
        }

        private void LoadData()
        {
            JObject paramObject = new JObject();
            if (!queryBeginDate.SelectedDate.ToString().Trim().Equals(""))
            {
                paramObject.Add("queryBeginDate", ((DateTime)queryBeginDate.SelectedDate).ToString("yyyy-MM-dd"));
            }
            if (!queryEndDate.SelectedDate.ToString().Trim().Equals(""))
            {
                paramObject.Add("queryEndDate", ((DateTime)queryEndDate.SelectedDate).ToString("yyyy-MM-dd"));
            }
            if (!queryCarNo.Text.ToString().Trim().Equals(""))
            {
                paramObject.Add("queryCarNo", queryCarNo.Text.ToString());
            }
            paramObject.Add("status", queryBillStatus);
            paramObject.Add("deviceCode", deviceCode);

            //new Task(() =>
            //{
            try
            {
                String serverIp = App.GetConfig().HttpServerIp;
                String serverPort = App.GetConfig().HttpServerPort;
                String response = HttpPostHelper.doHttpPost("http://" + serverIp + ":" + serverPort + "/qd-s-factory/clientApi/queryBillsForWeight", paramObject.ToString());
                JObject rspJson = JObject.Parse(response);
                string code = rspJson["code"].ToString();
                if (code.ToLower() == "00000")
                {
                    var jsonStr = rspJson["data"].ToString();
                    List<BillModel> bills = JsonConvert.DeserializeObject<List<BillModel>>(jsonStr);

                    _syncContext.Post(SetDataList, bills);
                }
            }
            catch (Exception ex)
            {

            }

            //}).Start();
        }
        private void SetDataList(object obj)
        {
            mainViewModel.List.Clear();

            if (null == obj) return;
            List<BillModel> bills = (List<BillModel>)obj;

            foreach (BillModel bill in bills)
            {
                double qian = 0;
                double hou = 0;
                //bill.chazhi = (bill.MaoWeight-bill.WaiMaoWeight).ToString("0.00");
                if ( bill.NetWeight != 0)
                {
                    //qian = double.Parse(bill.FySurfaceFront);
                    //hou = double.Parse(bill.FySurfaceBack);
                    //bill.chazhi = (bill.NetWeight * 1000 - (hou - qian)).ToString("0.00");
                    bill.netLoss=(bill.NetWeight - bill.WaiNetWeight).ToString("0.00");
                }
                if (bill.MaoWeight != 0)
                {
                    bill.chazhi = (bill.MaoWeight - bill.WaiMaoWeight).ToString("0.00");

                }

                mainViewModel.List.Add(bill);
            }
        }
        private void Button_Click_ClearBillInfo(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = System.Windows.MessageBox.Show("是否清除称重数据?", "确认", MessageBoxButton.YesNo);
            if (result != MessageBoxResult.Yes)
            {
                return;
            }
            JObject jsonObj = new JObject();
            jsonObj.Add("from", "monitor");
            jsonObj.Add("type", "WorkComplete");
            //sendClientMqMessage(viewModel.Item01.Id, jsonObj);
        }
        private void Button_Click_PrintWeighOrder(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = System.Windows.MessageBox.Show("是否选择打印磅单?", "确认", MessageBoxButton.YesNo);
            if (result != MessageBoxResult.Yes)
            {
                return;
            }
            //MainWindow main  = new MainWindow();
            BillModel bill = (BillModel)PoundList.SelectedItem;
            main.rePrint(bill);       
        }

        private void Button_Click_Query(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void button_click_queryReset(object sender, RoutedEventArgs e)
        {
            resetQueryParamaters();
        }
        private void resetQueryParamaters()
        {
            queryBeginDate.SelectedDate = DateTime.Now;//.ToString("yyyy-MM-dd");
            queryEndDate.SelectedDate = DateTime.Now;//.ToString("yyyy-MM-dd");
            queryCarNo.Text = "";
        }

        private void Button_Click_OneImage(object sender, RoutedEventArgs e)
        {
            BillModel bill = (BillModel)PoundList.SelectedItem;
            try
            {
                System.Diagnostics.Process.Start(App.GetConfig().ImagePath + bill.tareOneImg);
            }
            catch (Exception ex) { }
        }

        private void Button_Click_TwoImage(object sender, RoutedEventArgs e)
        {
            BillModel bill = (BillModel)PoundList.SelectedItem;
            try
            {
                System.Diagnostics.Process.Start(App.GetConfig().ImagePath + bill.tareTwoImg);
            }
            catch (Exception ex) { }
        }
        private void Button_Click_ReWeigh(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = System.Windows.MessageBox.Show("是否选择复磅?", "确认", MessageBoxButton.YesNo);
            if (result != MessageBoxResult.Yes)
            {
                return;
            }
            String serverIp = App.GetConfig().HttpServerIp;
            String serverPort = App.GetConfig().HttpServerPort;
            BillModel bill = (BillModel)PoundList.SelectedItem;
            JObject json = new JObject();
            json.Add("id", bill.Id);
            String response = HttpPostHelper.doHttpPost("http://" + serverIp + ":" + serverPort + "/qd-s-factory/clientApi/fubang", json.ToString());
            JObject rspJson = JObject.Parse(response);
            string code = rspJson["code"].ToString();
            if (code.ToLower() == "00000")
            {
                JObject jsonObj = new JObject();
                jsonObj.Add("from", "monitor");
                jsonObj.Add("type", "reWeigh");
                //new MainWindow().sendClientMqMessage(viewModel.Item01.Id, jsonObj);
                MessageBox.Show("复磅成功");
                LoadData();
            }

        }

        private void Button_Click_BillImg(object sender, RoutedEventArgs e)
        {
            BillModel bill = (BillModel)PoundList.SelectedItem;
            try
            {
                System.Diagnostics.Process.Start(bill.BillUploadImg);
            }
            catch (Exception ex) { }
        }
    }
}
